package com.ruoyi.prod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.prod.entity.po.ProdUseApply;
import com.ruoyi.prod.entity.po.ProdUseApplyItem;
import com.ruoyi.prod.mapper.ProdUseApplyItemMapper;
import com.ruoyi.prod.service.ProdUseApplyItemService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 使用管理-使用申请-条目明细 服务实现类
 * </p>
 *
 * @author 强子初
 * @since 2023-02-25
 */
@Service
public class ProdUseApplyItemServiceImpl extends ServiceImpl<ProdUseApplyItemMapper, ProdUseApplyItem> implements ProdUseApplyItemService {

    @Override
    public ProdUseApplyItem getByAssetId(String assetId) {
        LambdaQueryWrapper<ProdUseApplyItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdUseApplyItem::getAssetId, assetId);
        return this.getOne(wrapper);
    }

    @Override
    public List<ProdUseApplyItem> list(String applyId) {
        LambdaQueryWrapper<ProdUseApplyItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdUseApplyItem::getApplyId, applyId);
        return this.list(wrapper);
    }

    @Override
    public boolean add(List<ProdUseApplyItem> pos) {
        pos.forEach(po -> po.setUseState("1"));
        this.saveOrUpdateBatch(pos);
        return true;
    }

    @Override
    public boolean add(ProdUseApply useApply, List<ProdUseApplyItem> pos) {

        // 设置详细条目的来自大表的基本信息
        pos.forEach(po -> {
            po.setApplyId(useApply.getId());
            po.setApplyOdd(useApply.getOdd());
        });
        return this.add(pos);
    }

    @Override
    public boolean deleteByAssetId(String assetId) {
        LambdaQueryWrapper<ProdUseApplyItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdUseApplyItem::getAssetId, assetId);
        return this.remove(wrapper);
    }
}